package com.tailyou.androidlearning2426.act

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.tailyou.androidlearning2426.R
import com.tailyou.androidlearning2426.act.ui.theme.AndroidLearning2400Theme

class RollDiceActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AndroidLearning2400Theme {
                Surface(modifier = Modifier.fillMaxSize()) {
                    RollDiceView()
                }
            }
        }
    }
}

@Composable
fun RollDiceView() {
    // result变化会触发界面胡刷新
    var result by remember { mutableIntStateOf(1) }
    // Column 列，容器组件
    val resId = when (result) {
        1 -> R.drawable.dice_1
        2 -> R.drawable.dice_2
        3 -> R.drawable.dice_3
        4 -> R.drawable.dice_4
        5 -> R.drawable.dice_5
        else -> R.drawable.dice_6
    }
    Column(
        modifier = Modifier.fillMaxSize(), //大小填充屏幕
        horizontalAlignment = Alignment.CenterHorizontally,//水平方向对齐方式
        verticalArrangement = Arrangement.Center//垂直方向对齐方式
    ) {
        // Image 图片组件
        Image(
            painter = painterResource(resId),//设置图片资源
            contentDescription = null,//图片内容描述，必须
            modifier = Modifier.size(256.dp, 256.dp)//设置图片组件宽高
        )

        // Button 按钮组件
        Button(onClick = {
            // 处理按钮的点击事件
            result = (1..6).random()
        }) {
            // Text 文本组件
            Text("Roll")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun UIPreview() {
    AndroidLearning2400Theme {
        RollDiceView()
    }
}